package com.example.traning.mall.dal.mysql.dict;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.traning.mall.dal.dataobject.dict.DictDistrict;
import com.example.traning.mall.framework.mybatis.core.mapper.BaseMapperX;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 省市区字典 Mapper 接口
 * </p>
 *
 * @author Lei Yu
 * @since 2025-04-29
 */
@Mapper
public interface DictDistrictMapper extends BaseMapperX<DictDistrict> {

    /**
     * 默认方法：直接使用条件构造器，构造 sql
     * <p/>
     * 根据父级查询子级地区列表，sql：select id, code, name, pinyin from dict_district where parent_id = ?
     *
     * @param parentId 父级地区ID
     * @return 子级地区列表
     */
    default List<DictDistrict> selectByParentId(String parentId) {
        // Lambda 风格的条件构造器： LambdaQueryWrapper
        LambdaQueryWrapper<DictDistrict> wrapper = new LambdaQueryWrapper<>();
        // select id, code, name, pinyin
        wrapper.select(DictDistrict::getId, DictDistrict::getCode, DictDistrict::getName, DictDistrict::getPinyin);
        // where parent_id = ?
        wrapper.eq(DictDistrict::getParentId, parentId);
        return selectList(wrapper);
    }


    /**
     * 同 {@link DictDistrictMapper#selectByParentId}，也可以用 xml 方式配置 sql
     */
    @Select("""
            select id, code, name, pinyin from dict_district where parent_id = #{parentId}
            """)
    List<DictDistrict> selectByParentIdUserSql(String parentId);

}
